<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="200px">
      <el-form-item label="主单号" prop="purchaseRequestId">
        <el-input
          v-model="queryParams.purchaseRequestId"
          placeholder="请输入主单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="需求时间">
        <el-date-picker
          v-model="daterangeDemandTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="需求类型" prop="demandType">
        <el-select v-model="queryParams.demandType" placeholder="请选择需求类型" clearable>
          <el-option
            v-for="dict in dict.type.a_demand_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="流程状态" prop="processStatus">
        <el-select v-model="queryParams.processStatus" placeholder="请选择流程状态" clearable>
          <el-option
            v-for="dict in dict.type.a_process_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请人" prop="createUserId">
        <el-input
          v-model="queryParams.createUserName"
          placeholder="申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['asset:request:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="requestList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主单号" align="center" prop="purchaseRequestId" />
      <el-table-column label="需求时间" align="center" prop="demandTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.demandTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="需求类型" align="center" prop="demandType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.a_demand_type" :value="scope.row.demandType"/>
        </template>
      </el-table-column>
      <el-table-column label="流程状态" align="center" prop="processStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.a_process_status" :value="scope.row.processStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="createUserName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="queryDetail(scope.row)"
            v-hasPermi="['asset:plan:edit']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listRequest, getRequest, delRequest, addRequest, updateRequest } from "@/api/asset/request";

export default {
  name: "Request",
  dicts: ['a_demand_type', 'a_process_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedFaPurchaseRequestDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购申请表格数据
      requestList: [],
      // ${subTable.functionName}表格数据
      faPurchaseRequestDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 入库状态入库状态(0:全部未入库,1:部分入库,2:全部入库)时间范围
      daterangeDemandTime: [],
      // 入库状态入库状态(0:全部未入库,1:部分入库,2:全部入库)时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        purchaseRequestId: null,
        demandTime: null,
        demandType: null,
        processStatus: null,
        createUserName: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询采购申请列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeDemandTime && '' != this.daterangeDemandTime) {
        this.queryParams.params["beginDemandTime"] = this.daterangeDemandTime[0];
        this.queryParams.params["endDemandTime"] = this.daterangeDemandTime[1];
      }
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listRequest(this.queryParams).then(response => {
        this.requestList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        purchaseRequestId: null,
        demandTime: null,
        demandType: null,
        pdfPath: null,
        processId: null,
        processStatus: null,
        createUserId: null,
        createTime: null,
        updateUserId: null,
        updateTime: null
      };
      this.faPurchaseRequestDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDemandTime = [];
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.purchaseRequestId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const purchaseRequestId = row.purchaseRequestId || this.ids
      getRequest(purchaseRequestId).then(response => {
        this.form = response.data;
        this.faPurchaseRequestDetailList = response.data.faPurchaseRequestDetailList;
        this.open = true;
        this.title = "修改采购申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.faPurchaseRequestDetailList = this.faPurchaseRequestDetailList;
          if (this.form.purchaseRequestId != null) {
            updateRequest(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRequest(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const purchaseRequestIds = row.purchaseRequestId || this.ids;
      this.$modal.confirm('是否确认删除采购申请编号为"' + purchaseRequestIds + '"的数据项？').then(function() {
        return delRequest(purchaseRequestIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** ${subTable.functionName}序号 */
    rowFaPurchaseRequestDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** ${subTable.functionName}添加按钮操作 */
    handleAddFaPurchaseRequestDetail() {
      let obj = {};
      obj.planDetailId = "";
      obj.largeCategoryCode = "";
      obj.middleCategoryCode = "";
      obj.smallCategoryCode = "";
      obj.productName = "";
      obj.specificationModle = "";
      obj.measurementUnit = "";
      obj.unitPrice = "";
      obj.procureQuantity = "";
      obj.budgetAmount = "";
      obj.procurePurpose = "";
      obj.productLineId = "";
      obj.remarks = "";
      obj.supplierId = "";
      obj.receivedQuantity = "";
      obj.unreceivedQuantity = "";
      obj.warehousingStatus = "";
      this.faPurchaseRequestDetailList.push(obj);
    },
    /** ${subTable.functionName}删除按钮操作 */
    handleDeleteFaPurchaseRequestDetail() {
      if (this.checkedFaPurchaseRequestDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的${subTable.functionName}数据");
      } else {
        const faPurchaseRequestDetailList = this.faPurchaseRequestDetailList;
        const checkedFaPurchaseRequestDetail = this.checkedFaPurchaseRequestDetail;
        this.faPurchaseRequestDetailList = faPurchaseRequestDetailList.filter(function(item) {
          return checkedFaPurchaseRequestDetail.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleFaPurchaseRequestDetailSelectionChange(selection) {
      this.checkedFaPurchaseRequestDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('asset/request/export', {
        ...this.queryParams
      }, `request_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
